package com.component;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.game.model.BasePlayer;

import net.sf.json.JSONObject;


public class Util {

	@SuppressWarnings("rawtypes")
	public static void setMethod(Object obj, String filed, String value)
			throws Exception {
		try {
			Object a = null;
			Class clazz = obj.getClass();
			PropertyDescriptor pd = new PropertyDescriptor(filed, clazz);
			Method getMethod = pd.getWriteMethod();
			Class paramType = getMethod.getParameterTypes()[0];
			if (paramType.getName().equals(Integer.class.getName())) {
				a = Integer.valueOf(value);
			} else if (paramType.getName().equals(Long.class.getName())){
				a = Long.valueOf(value);
			} else {
				a = value;
			}
			
			if (pd != null) 
				getMethod.invoke(obj, a);
			
		} catch (SecurityException e) {
			throw e;
		} catch (IllegalArgumentException e) {
			throw e;
		} catch (IntrospectionException e) {
			throw e;
		} catch (IllegalAccessException e) {
			throw e;
		} catch (InvocationTargetException e) {
			throw e;
		}
	}

	@SuppressWarnings("rawtypes")
	public static Object doMethod(Object obj, String filed, Object... values) {
		try {
			Class<? extends Object> clazz = obj.getClass();
			Class[] params = new Class[values.length];
			for (int i = 0; i < values.length; i++) {
				Class bClass = values[i].getClass();
				if (values[i] instanceof Integer) {
					params[i] = int.class;
				} else if (values[i] instanceof Float) {
					params[i] = float.class;
				} else if (values[i] instanceof Double) {
					params[i] = double.class;
				} else if (values[i] instanceof Long) {
					params[i] = long.class;
				} else if (values[i] instanceof Byte) {
					params[i] = byte.class;
				} else {
					params[i] = values[i].getClass();
				}
			}
			Method getMethod = clazz.getMethod(filed, params);

			return (Object) getMethod.invoke(obj, values);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}
	public static JSONObject isPassEntryLimit(BasePlayer player, String entryLimitStr) {
		JSONObject jsonObject = new JSONObject();
		if (entryLimitStr == null || entryLimitStr.equals("")) {
			jsonObject.put("flag", true);
			return jsonObject;
		}
		String[] contentArr = entryLimitStr.split("[$]");
		for (int i = 0; i < contentArr.length; i++) {
			String[] entryLimitArr = contentArr[i].split(":");
			int propId = Integer.valueOf(entryLimitArr[0]);
			int num = Integer.valueOf(entryLimitArr[1]);
			Long curValue = player.getBagInfo().getLong(propId + "");
			if (curValue < Math.abs(num)) {
				jsonObject.put("flag", false);
				jsonObject.put("msg", getPropNameById(propId) + "不足" + Math.abs(num));
				return jsonObject;
			}
		}
		jsonObject.put("flag", true);
		return jsonObject;
	}
	public static String getPropNameById(int id) {
		switch (id) {
		case 1:
			return "钻石";
		case 2:
			return "金币";
		case 3:
			return "欢乐场入场券";
		case 4:
			return "Q币场入场券";
		case 5:
			return "话费场入场券";
		case 6:
			return "话费兑换券";
		case 7:
			return "QB兑换券";
		case 8:
			return "补签卡";
		case 9:
			return "小喇叭";
		}

		return "";
	}
}
